package 剑指Offer;

public class Offer21_调整数组顺序使奇数位于偶数前面 {
    public int[] exchange(int[] nums) {
        if (nums.length == 0 || nums == null) {
            return new int[]{};
        }
        int left = 0;
        int right = nums.length - 1;
        int temp = 0;

        while (left <= right) {
            if (left == right) {//这题l、r重合了就可以结束了
                break;
            }

            // 指针 i 遇到奇数则执行 i = i + 1 跳过，直到找到偶数；
            while (left < right && (nums[left] % 2) == 1) {
                left++;
            }
            // 指针 j 遇到偶数则执行 j = j - 1 跳过，直到找到奇数；
            while (left < right && (nums[right] % 2) == 0) {
                right--;
            }
            temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
        }
        return nums;
    }
}
